﻿
using Code4Bugs.Utils.Types;
using DataBase.DBmodels;
using DataBase.Utils;
using GreenXi.Model;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace GreenXi.ViewModel
{

    public class CraftViewModel : CraftModel
    {
        private static int PressId = 1;//压机ID
        private static int crafttype = 1; //组ID

        private Pages.Setting.Craft_Edit CraftEdit;

        private void RefreshCraftTypeList()
        {
            Init();
            using MyDbContext myDbContext = new();
        }

        private void RefreshCraftList(int craftTypeId)
        {
            using MyDbContext myDbContext = new();
            crafttype = craftTypeId;
            var CraftLists = myDbContext.GetCraftByType(1);
            foreach (var item in CraftLists)
            {
                CraftList.Add(item);
            }
        }

        public void Init()
        {
            using MyDbContext myDbContext = new();
            Crafts = new DataBase.DBmodels.Craft();
            RegionList = new(myDbContext.GetRegion());
            GatewayList = new(myDbContext.GetGateway());
            CraftList = new ObservableCollection<Craft>();
            
                var CraftLists = myDbContext.GetCraftByType(1);
                foreach (var item in CraftLists)
                {
                    CraftList.Add(item);
                }
           
            SportsMode = new ObservableCollection<string>
            {
                "环境显示",
                "总表数据",
             
            };
            if (Crafts.ShowType == "0")
            {
                Power = Visibility.Hidden;
                Displacement = Visibility.Hidden;
            }
            if (PressId == 1)
            {
                Press1 = false;
                Press2 = true;
            }
            else
            {
                Press1 = true;
                Press2 = false;
            }



        }
        public CraftViewModel()
        {
            Init();//初始化
    
      



            EditColumn = new DelegateCommand<object>((p) => { EditColumn_Click(p); });//编辑栏目
            SaveColumn = new DelegateCommand<object>((p) => { SaveColumn_Click(p); });//保存栏目

            CloseWindows = new DelegateCommand<object>((p) => { CloseWindows_Click(); });//关闭编辑窗口

            SwitchPress = new DelegateCommand(() => { SwitchPress_Click(); }); //切换压机

    
            DelProject = new DelegateCommand<object>((p) => { DelProject_Click(p); });//删除项目
            AddProject = new DelegateCommand(() => { AddProject_Click(); });          //新增项目
            EditProject = new DelegateCommand<object>((p) => { EditProject_Click(p); });//编辑项目
            SaveProject = new DelegateCommand<object>((p) => { SaveProject_Click(p); });//保存项目
        }

       

        #region 栏目
        private void EditColumn_Click(Object param)
        {
            RefreshCraftTypeList();
            var paramss = param as Craft;
            Crafts = paramss;
            CraftEdit = new Pages.Setting.Craft_Edit();
            CraftEdit.DataContext = this;
            CraftEdit.ShowDialog();
        }




        private void SaveColumn_Click(Object param)
        {
            //判断主表是否超过五个
            int maincraftcount = GlobalVariables.CraftViewList.Where(x => x.MainCraft!=null&& x.MainCraft.Value).Count();
            if(maincraftcount>4&& Crafts.MainCraft.Value)
            {
                MessageBox.Show("主表不能超过5个！");
                return;
            }
            using MyDbContext myDbContext = new();
            myDbContext.UpdateCraft(Crafts);
            GlobalVariables.CraftViewList = MyDbContext.GetCraftViewByType(5);
            MessageBox.Show("修改成功！");
            CraftEdit.Close();//关闭弹窗
        }

        private void CloseWindows_Click()
        {
            CraftEdit.Close();//关闭弹窗
        }

    
      
        #endregion

        #region 项目
        private void EditProject_Click(Object param)
        {
            var paramss = param as Craft;
            Crafts = paramss;
            Pages.Setting.Craft_Edit CraftEdit = new();
            CraftEdit.DataContext = this;
            CraftEdit.ShowDialog();
        }
   
        // Swap 方法用于交换数组中两个位置的对象
        void Swap(List<Craft> array, int index1, int index2)
        {
            if (array == null)
            {
                return;
            }
            int tmpId = array[index1].Id;
            array[index1].Id = array[index2].Id;
            array[index2].Id = tmpId;
        }

        private void MoveProjectDown_Click(Object param)
        {
            var craft = param as Craft;
            using MyDbContext myDbContext = new();
            var CraftLists = myDbContext.GetCraftByType(craft.Type ?? 0);
            var currentIndex = CraftLists.FindIndex(x => x.Id == craft.Id);

            // 下移对象
            if (currentIndex < CraftLists.Count)
            {
                int targetIndex = currentIndex + 1;
                Swap(CraftLists, currentIndex, targetIndex);
            }
            myDbContext.Craft.UpdateRange(CraftLists);
            myDbContext.SaveChanges();
            RefreshCraftList(craft.Type ?? 0);
        }
        private void SaveProject_Click(Object param)
        {
            using MyDbContext myDbContext = new();
            myDbContext.UpdateCraft(Crafts);
        }
        private async void AddProject_Click()
        {
            var craft = new Craft
            {
                Project = "电表名称",
                Speed = 50,
                StartPoint = 0,
                EndPoint = 0,
                Type = crafttype,
                ShowType = "环境显示"
            };
            CraftList.Add(craft);

            using MyDbContext myDbContext = new();
            await myDbContext.AddAsync<DataBase.DBmodels.Craft>(craft);

        }
        private async void DelProject_Click(object param)//删除项目
        {
            var paramss = param as Craft;
            using MyDbContext myDbContext = new();
            await myDbContext.DeleteAsync<Craft>(paramss);
            CraftList.Remove(paramss);
        }
        #endregion
        //压机切换
        private void SwitchPress_Click()
        {
            if (Press2)
            {
                PressId = 2;
            }
            else
            {
                PressId = 1;
            }
            Init();
        }
    }
}
